<template>
  <div>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row type="flex" justify="space-between">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="addFn">
              添加分类
            </el-button>
          </el-col>
          <el-col :span="6">
            <el-input v-model="title_contains" style="width:200px" placeholder="请输入关键字" />
            <el-button type="info" icon="el-icon-search " @click="filters" />
          </el-col>
        </el-row>
      </div>

      <!-- 表格 -->
      <el-table
        :data="tableData"
        border
      >
        <el-table-column
          prop="image"
          label="图片"
          width="120px"
        >
          <template v-slot="{row}">
            <img :src="row.image" label="图片" style="width:100px">
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
        />
        <el-table-column
          prop="desc"
          label="介绍"
        />
        <el-table-column
          prop="address"
          label="操作"
        >

          <template v-slot="{row}">
            <el-button type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="delFn(row.id)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>

      <el-row type="flex" justify="center">
        <!-- 分页器 -->
        <el-pagination
          layout="prev, pager, next"
          :total="total"
          :page-size="limit"
          @current-change="changeFn"
        />
      </el-row>
    </el-card>
    <!-- 增加分类 -->
    <addtypes ref="add" :show-dialog.sync="showDialog" @addEvent="getTypeList" />

  </div>
</template>

<script>
import addtypes from './components/addtypes.vue'
import { getTypes, delTypes, getTypesNum } from '@/api/types'
export default {
  components: {
    addtypes
  },
  data() {
    return {
      tableData: [],
      showDialog: false,
      // 名字
      cardInput: '',
      // 每页的获取条数
      limit: 2,
      // 开始获取的位置
      start: 0,
      total: 0,
      title_contains: ''

    }
  },
  async created() {
    await this.getTypesNum()

    this.getTypeList()
  },
  methods: {
    // 新增打开弹窗
    addFn() {
      // 显示弹窗
      this.showDialog = true
    },
    // 编辑打开弹窗
    editFn(id) {
      this.showDialog = true
      // 通过ref调用子组件函数并获取编辑项的id
      this.$refs.add.updateTypes(id)
    },
    // 渲染
    async getTypeList() {
      const res = await getTypes({
        _start: this.start,
        _limit: this.limit
      })
      // console.log(111, res)
      this.tableData = res.data
    },
    // 获取分页条数
    async getTypesNum() {
      const res = await getTypesNum()
      // console.log(11, res)
      this.total = res.data
    },
    // 删除
    async delFn(id) {
      this.$confirm('确认删除吗？', '删除', {
        type: 'warning'
      }).then(async() => {
        await delTypes(id)
        // 刷新列表
        this.getTypeList()
      }).catch(() => {
        console.log('取消了删除')
      })
    },
    // 分页
    changeFn(page) {
      this.start = page === 1 ? 0 : (page - 1) * this.limit
      // console.log(118, page)
      // 刷新页面
      this.getTypeList()
    },
    // 筛选
    async filters() {
      const res = await getTypes({
        _limit: this.limit,
        title_contains: this.title_contains
      })

      // 再次调用
      const res1 = await getTypesNum({
        title_contains: this.title_contains,
        total: res.total
      })
      this.tableData = res.data
      this.total = res1.data
      console.log(22, res1)
      
    }
  }
}
</script>

<style scoped>

</style>
